package com.sxz.sbs.module.telephone;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.event.OperateTelSuccessEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.telephone.adapter.TelPersonnelAdapter;
import com.sxz.sbs.module.telephone.bean.TelBookBean;
import com.sxz.sbs.module.telephone.presenter.SearchTelPresenter;
import com.sxz.sbs.module.telephone.view.SearchTelView;
import com.sxz.sbs.widget.PhoneDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索电话薄
 * @author hp
 */
public class SearchTelBookActivity extends BaseActivity<SearchTelPresenter> implements SearchTelView {


    @BindView(R.id.cet_search_tel)
    ClearEditText cetSearchTel;
    @BindView(R.id.tv_search_tel_cancel)
    TextView tvSearchTelCancel;
    @BindView(R.id.rv_search_tel)
    RecyclerView rvSearchTel;
    private List<TelBookBean.ManagerListBean> personnelData = new ArrayList<>();
    private TelPersonnelAdapter telPersonnelAdapter;
    @BindView(R.id.search_tel_state)
    StateLayout stateLayout;
    private int selectIndex;
    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        rvSearchTel.setLayoutManager(new LinearLayoutManager(this));
        telPersonnelAdapter = new TelPersonnelAdapter(personnelData);
        rvSearchTel.setAdapter(telPersonnelAdapter);
        cetSearchTel.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    InputMethodManager imm = (InputMethodManager) v.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive())
                        imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    if (TextUtils.isEmpty(cetSearchTel.getInputValue())) {
                        ToastUtils.showShort("搜索内容不能为空");
                        return false;
                    }
                    HashMap<String,Object> map = HttpParamMap.getParamMap();
                    map.put("keyWord",cetSearchTel.getInputValue());
                    mPresenter.getTelBook(map);
                    return true;
                }
                return false;
            }
        });
        rvSearchTel.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                selectIndex = position;
                startActivity(new Intent(SearchTelBookActivity.this,PersonageDetailsActivity.class)
                        .putExtra(IntentConstant.TEL_BOOK_DETAIL,personnelData.get(position)));
            }
        });
        rvSearchTel.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PhoneDialog.callPhone(SearchTelBookActivity.this,personnelData.get(position).getTxt_Phone());
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_tel_book;
    }

    @Override
    protected SearchTelPresenter createPresenter() {
        return new SearchTelPresenter();
    }


    @OnClick(R.id.tv_search_tel_cancel)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onGetSearchTelSuccess(List<TelBookBean.ManagerListBean> data) {
        if (mIsLive) {
            if (data != null && data.size() > 0) {
                stateLayout.showContent();
                personnelData.clear();
                personnelData.addAll(data);
                telPersonnelAdapter.notifyDataSetChanged();
            } else {
                stateLayout.showEmpty();
            }
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOperateSuccessEvent(OperateTelSuccessEvent event) {
        if (personnelData.get(selectIndex).getMark() == 1) {
            personnelData.get(selectIndex).setMark(2);
        } else {
            personnelData.get(selectIndex).setMark(1);
        }
        telPersonnelAdapter.notifyDataSetChanged();

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
